package com.anrongtec.zcpt.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.anrongtec.zcpt.ApiConfig;
import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.ShoppingFragmentpPagerAdapter;
import com.anrongtec.zcpt.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("ValidFragment")
public class ShoppingFragment extends BaseFragment implements View.OnClickListener {
    private TextView tv;
    private String name;


    private TabLayout tl_shopping;                            //定义TabLayout
    private ViewPager vp_shopping_pager;                             //定义viewPager
    private FragmentPagerAdapter shoppingAdapter;                               //定义adapter
    private Button btn_shopping_top_install;

    private List<Fragment> list_fragment;                                //定义要装fragment的列表
    private List<String> list_title;                                     //tab名称列表

    private AllAppFragment allAppFragment;                               //全部应用fragment
    private UpdateAppFragment updateAppFragment;                        //更新应用fragment
    private ManagerAppFragment managerAppFragment;                      //管理应用fragment  //tab名的列表


    public ShoppingFragment(String fName) {
        this.name = fName;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_shopping, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        tl_shopping = (TabLayout) view.findViewById(R.id.tab_shopping_indicator);
        vp_shopping_pager = (ViewPager) view.findViewById(R.id.vp_shopping_fragment);
        btn_shopping_top_install = (Button) view.findViewById(R.id.btn_shopping_top_install);

        allAppFragment = new AllAppFragment();
        updateAppFragment = new UpdateAppFragment();
        managerAppFragment = new ManagerAppFragment();

        //将fragment装进列表中
        list_fragment = new ArrayList<>();
        list_fragment.add(allAppFragment);
        list_fragment.add(updateAppFragment);
        list_fragment.add(managerAppFragment);

        btn_shopping_top_install.setOnClickListener(this);


    }

    private void initData() {
        //将名称加载tab名字列表，正常情况下，我们应该在values/arrays.xml中进行定义然后调用
        list_title = new ArrayList<>();
        list_title.add("全部应用");
        list_title.add("更新应用");
        list_title.add("应用管理");

        //设置TabLayout的模式  
        tl_shopping.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称  
        tl_shopping.addTab(tl_shopping.newTab().setText(list_title.get(0)));
        tl_shopping.addTab(tl_shopping.newTab().setText(list_title.get(1)));
        tl_shopping.addTab(tl_shopping.newTab().setText(list_title.get(2)));

        shoppingAdapter = new ShoppingFragmentpPagerAdapter(getActivity().getSupportFragmentManager(),list_fragment,list_title);

        //viewpager加载adapter
        vp_shopping_pager.setAdapter(shoppingAdapter);
        //tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
        //TabLayout加载viewpager
        tl_shopping.setupWithViewPager(vp_shopping_pager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_shopping_top_install:
                showToastCustom("暂未开通", ApiConfig.INFO);
        }
    }
}
